package cn.Controller;

import cn.CommonUtils.EhcacheFactory;
import cn.Dao.EmployeeDao;
import cn.DataResourceSecondComponent.Dao.StudentDao;
import cn.DataResourceSecondComponent.Entity.Student;
import cn.Entity.Employee;
import cn.Service.Impl.EmployeeServiceImpl;
import cn.Service.IEmployeeService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
//import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
@Slf4j
@Controller
public class SecondController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private EmployeeDao employeeDao;

    private EmployeeServiceImpl employeeServiceImpl;

//    @Autowired
//    private StudentDao studentDao;

/*    @ResponseBody
    @RequestMapping("/listAllEmployee")
    public List<Employee> listAllEmployeeProc() throws IOException {
        List<Employee> result = new LinkedList<>();

        CacheManager cacheManager = EhcacheFactory.getCacheManager();
        if(cacheManager.getStatus()!=Status.AVAILABLE){
            cacheManager.init();
        }

        Cache<Integer, Employee> cache = cacheManager.getCache("employeeCache", Integer.class, Employee.class);

        this.employeeService.findAll().forEach(i->{
            System.out.println(i.toString());
            if(cache!=null)
                cache.put(i.getId(),i);
        });

        if(cache!=null)
            cache.forEach(i->{
                result.add(i.getValue());
            });

      return result;
    }*/

    @ResponseBody
    @RequestMapping("/listAllEmployee2")
    public String listAllEmployeeProc2() throws IOException {

        JSONArray result = new JSONArray();

        this.employeeService.findAll().forEach(i->{
            System.out.println(i.toString());
            JSONObject jsonObject = new JSONObject();
            result.add( JSONObject.toJSONString(i));
        });
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping("/listAllEmployee3")
    public List<Employee> listAllEmployees3Proc(){
        return this.employeeService.findAll();
    }


    @ResponseBody
    @RequestMapping("/getEmployee4")
    public Employee listAllEmployees4Proc(Integer id){

        return this.employeeService.getOne(id);
//        return this.employeeService.findAll();
    }


    @ResponseBody
    @RequestMapping("/listAllInCache")
    public List getem(){
        List<Employee> result = new LinkedList<>();
        CacheManager cacheManager = EhcacheFactory.getCacheManager();
        if(cacheManager.getStatus()!=Status.AVAILABLE){
            cacheManager.init();
        }

        Cache<Integer, Employee> cache = cacheManager.getCache("employeeCache", Integer.class, Employee.class);
        if(cache!=null)
        cache.forEach(i->{
            result.add(i.getValue());
        });
//        cache.clear();
        return result;
    }

    @ResponseBody
    @RequestMapping("/testTx")
    public void testTx(){

/*        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");

        EmployeeDao employeeDaoTx = sessionFactory.openSession().getMapper(EmployeeDao.class);

        Employee employee = employeeDaoTx.selectOne(1500);
        Employee employee2 = employeeDaoTx.selectOne(30);
        System.out.println(employee.toString());*/

    }

    @ResponseBody
    @RequestMapping("/testTx2")
    public void testTxProc(Integer begin , Integer end){
    /*    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
        EmployeeServiceImpl employeeService = (EmployeeServiceImpl) applicationContext.getBean("employeeServiceImpl");

        EmployeeDao employeeDaoTx = sessionFactory.openSession().getMapper(EmployeeDao.class);
        employeeService.setEmployeeDao(employeeDaoTx);
        try {
            employeeService.batchDeleteEmployee(begin, end);
        }catch (IllegalStateException e){
            System.out.println(e.getMessage());
        }finally {
            System.out.println("finally");
        }*/
    }

/*    @ResponseBody
    @RequestMapping("/testAnnoDao")
    public List<Employee> listSomeEmployee(Integer begin , Integer count){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
        EmployeeServiceImpl employeeService = (EmployeeServiceImpl) applicationContext.getBean("employeeServiceImpl");
        Employee employee = employeeService.getOne(begin);
        List<Employee> employees = new LinkedList<>();
        employees.add(employee);
        return employees;
    }*/

    @ResponseBody
    @RequestMapping("/testTx3")
    public List<Employee> listSomeEmployee3(Integer begin , Integer count){
        Employee employee = employeeService.getOne(begin);
        List<Employee> employees = new LinkedList<>();
        employees.add(employee);
        return employees;
    }

    @PostConstruct
    public void ConstructProc(){
      /*  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
        this.employeeServiceImpl = (EmployeeServiceImpl) applicationContext.getBean("employeeServiceImpl");*/
    }

    @ResponseBody
    @RequestMapping("testmp1")
    public List<Employee> testMpProc(){
        log.info("come in test mp 1 func");
        Float f1 = 12000f;
        Float f2 = 13000f;
        Wrapper<Employee> wrapper = new QueryWrapper<Employee>().between("salary",  f1, f2);
        return this.employeeDao.selectList(wrapper);
    }

/*    @ResponseBody
    @RequestMapping("testmp2")
    public List<Student> testMpProc2(){
//        Float f1 = 12000f;
//        Float f2 = 13000f;
//        Wrapper<Employee> wrapper = new QueryWrapper<Employee>().between("salary",  f1, f2);
//        return this.studentDao.list();
    }*/

}
